package com.tj.collection.integration.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.base.Strings;
import com.qianli.cashmama.product.facade.result.AppPackageInfo;
import com.tj.collection.enums.MessageSceneEnum;
import com.tj.collection.integration.MessageIntegrationService;
import com.tj.collection.integration.UserEmailService;
import com.tj.collection.integration.dto.LoanNoteDto;
import com.tj.collection.integration.dto.UserAuthEmail;
import com.tj.collection.utils.ConfigUtil;
import com.tj.collection.utils.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/9/28
 * Time: 上午9:33
 */
@Service
public class MessageIntegrationServiceImpl implements MessageIntegrationService {

    private static final Log LOGGER = LogFactory.getLog(MessageIntegrationServiceImpl.class);

    @Autowired
    private MessageUtils messageUtils;
    @Autowired
    private ConfigUtil configUtil;

    @Override
    public boolean loanNote(LoanNoteDto loanNoteDto) {
//        try {
//            LOGGER.info("放款发送短信参数,loanNoteDto:{}", JSON.toJSONString(loanNoteDto));
//            SingleSmsSendRequestRO singleSmsSendRequestRO = new SingleSmsSendRequestRO();
//            singleSmsSendRequestRO.setPhone(loanNoteDto.getMobile());
//
//            Map<String, String> replaceParam = new HashMap<>();
//            if (loanNoteDto.isLoanSuccess()) {
//                String messageBizCode = messageUtils.getMessageBizCodeByPackageName(loanNoteDto.getUserCode(), loanNoteDto.getTradeNo(), MessageSceneEnum.SMS_PAYMENT_NONE);
//                singleSmsSendRequestRO.setBizCode(messageBizCode);
//                replaceParam.put("name", loanNoteDto.getUserName());
////                replaceParam.put("totalAmount", loanNoteDto.getBorrowCapital());
//                replaceParam.put("bankNo", loanNoteDto.getBankNo().substring(loanNoteDto.getBankNo().length() - 4));
////                replaceParam.put("obtainAmount", loanNoteDto.getRecievedCapital());
//            } /*else {
//                singleSmsSendRequestRO.setBizCode("CASHMAMA_0011");
//                replaceParam.put("name", loanNoteDto.getUserName());
//            }
//*/
//            singleSmsSendRequestRO.setReplaceParam(replaceParam);
//            singleSmsSendRequestRO.setUserCode(loanNoteDto.getUserCode());
//
//            LOGGER.info("== 放款发送短信参数 ==,singleSmsSendRequestRO:{}", JSON.toJSONString(singleSmsSendRequestRO));
//            return smsMsgService.send(singleSmsSendRequestRO).getData();
//        } catch (Exception e) {
//            LOGGER.error("== 放款结果短信发送失败 ==,loanNoteDto:{}", JSON.toJSONString(loanNoteDto));
//            return false;
//        }

        return true;
    }

    @Override
    public boolean loanNoteByEmail(String userCode, String content, String title) {
        return this.loanNoteByEmail(userCode, null, content, title);
    }

    @Override
    public boolean loanNoteByEmail(String userCode, String tradeNo, String content, String title) {
//        try {
//            AppPackageInfo appPackageInfo = messageUtils.getAppPackageInfoByUserCode(userCode, tradeNo);
//            String appCode = appPackageInfo.getAppCode();
//            if (!configUtil.getNeedSendEmailAppCode().contains(appCode)) {
//                LOGGER.info("非cashmama不发邮件2,{},userCode:{},appCode:{}", configUtil.getNeedSendEmailAppCode().toString(), userCode, appCode);
//                return false;
//            }
//
//            UserAuthEmail authEmail = userEmailService.getUserEmail(userCode);
//            content = content.replace("__AppName__", Strings.nullToEmpty(appPackageInfo.getDisplayName()));
//            content = content.replace("__Email Id__", Strings.nullToEmpty(authEmail.getEmail()));
//            content = content.replace("__Date__", DateUtil.getDate(new Date()));
//            SingleSmsSendRequestRO singleSmsSendRequestRO = new SingleSmsSendRequestRO();
//            LOGGER.info("loanNoteByEmail,loan note by email,userCode:{}, content{}, title:{}, email:{} ------", userCode, content, title, JSONObject.toJSONString(authEmail));
//            singleSmsSendRequestRO.setPhone(authEmail.getEmail());
//            Map<String, String> replaceParam = new HashMap<>();
//            singleSmsSendRequestRO.setBizCode(messageUtils.getMessageBizCodeByPackageName(userCode, tradeNo, MessageSceneEnum.COMMON_EMAIL));
//            singleSmsSendRequestRO.setTitle(title);
//            replaceParam.put("content", content);
//            singleSmsSendRequestRO.setReplaceParam(replaceParam);
//            singleSmsSendRequestRO.setUserCode(userCode);
//            LOGGER.info("loanNoteByEmail,发送邮件参数1,singleSmsSendRequestRO:{}", JSON.toJSONString(singleSmsSendRequestRO));
//            return emailSendService.singleSend(singleSmsSendRequestRO).getData();
//        } catch (Exception e) {
//            LOGGER.error("loanNoteByEmail,发送邮件失败1,userCode:{}, content:{}", userCode, content, e);
//            return false;
//        }

        return true;
    }

    /**
     * 发客服邮件
     *
     * @param userCode
     * @param content
     * @param title
     * @param email
     * @return
     */
    @Override
    public boolean sendCustomerServiceEmail(String userCode, String content, String title, String email) {
//        try {
//            AppPackageInfo appPackageInfo = messageUtils.getAppPackageInfoByUserCode(userCode, null);
//            String appCode = appPackageInfo.getAppCode();
//            if (!configUtil.getNeedSendEmailAppCode().contains(appCode)) {
//                LOGGER.info("非cashmama不发邮件2,{},userCode:{},appCode:{}", configUtil.getNeedSendEmailAppCode().toString(), userCode, appCode);
//                return false;
//            }
//
//            LOGGER.info("发客服邮件,email:{}", email);
//
//            if (StringUtils.isEmpty(email)) {
//                email = userEmailService.getUserEmail(userCode).getEmail();
//            }
//
//            content = content.replace("__AppName__", Strings.nullToEmpty(appPackageInfo.getDisplayName()));
//            content = content.replace("__Email Id__", Strings.nullToEmpty(email));
//            content = content.replace("__Date__", DateUtil.getDate(new Date()));
//            SingleSmsSendRequestRO singleSmsSendRequestRO = new SingleSmsSendRequestRO();
//            LOGGER.info("发客服邮件,loan note by email,userCode:{}, content{}, title:{}, email:{} ------", userCode, content, title, email);
//            singleSmsSendRequestRO.setPhone(email);
//
//            Map<String, String> replaceParam = new HashMap<>();
//            singleSmsSendRequestRO.setBizCode(messageUtils.getMessageBizCodeByPackageName(userCode, MessageSceneEnum.CUSTOMER_EMAIL));
//            singleSmsSendRequestRO.setTitle(appPackageInfo.getDisplayName().toUpperCase() + " " + title);
//            replaceParam.put("content", content);
//            singleSmsSendRequestRO.setReplaceParam(replaceParam);
//            singleSmsSendRequestRO.setUserCode(userCode);
//            LOGGER.info("发客服邮件,发送邮件参数2,singleSmsSendRequestRO:{}", JSON.toJSONString(singleSmsSendRequestRO));
//            return emailSendService.singleSend(singleSmsSendRequestRO).getData();
//        } catch (Exception e) {
//            LOGGER.error("发客服邮件,发送邮件失败2,userCode:{}, content:{}", userCode, content, e);
//            return false;
//        }

        return true;
    }

    /**
     * 手动发送邮件通知
     *
     * @param email
     * @param content
     * @param title
     * @param userCode
     * @return
     */
    @Override
    public boolean loanNote30ByEmail(String email, String content, String title, String userCode) {
//        try {
//            content = content.replace("__Email Id__", Strings.nullToEmpty(email));
//            content = content.replace("__Date__", DateUtil.getDate(new Date()));
//            SingleSmsSendRequestRO singleSmsSendRequestRO = new SingleSmsSendRequestRO();
//            LOGGER.info("loanNote30ByEmail,loan note by email,content{},title:{},email:{},userCode:{}", content, title, email, userCode);
//            singleSmsSendRequestRO.setPhone(email);
//            Map<String, String> replaceParam = new HashMap<>();
//            singleSmsSendRequestRO.setBizCode(messageUtils.getMessageBizCodeByPackageName(userCode, MessageSceneEnum.COMMON_EMAIL));
//            singleSmsSendRequestRO.setTitle(title);
//            replaceParam.put("content", content);
//            singleSmsSendRequestRO.setReplaceParam(replaceParam);
//            singleSmsSendRequestRO.setUserCode(userCode);
//            LOGGER.info("loanNote30ByEmail,手动发送邮件参数,singleSmsSendRequestRO:{}", JSON.toJSONString(singleSmsSendRequestRO));
//            return emailSendService.singleSend(singleSmsSendRequestRO).getData();
//        } catch (Exception e) {
//            LOGGER.error("loanNote30ByEmail,手动发送邮件失败,email:{}, content:{}", email, content, e);
//            return false;
//        }

        return true;
    }

//    @Override
//    public Result<Boolean> send(SingleSmsSendRequestRO singleSmsSendRequestRO) {
//        return smsMsgService.send(singleSmsSendRequestRO);
//    }

//    @Override
//    public Result<Boolean> batchPush(BatchPushSendRequestRO batchPushSendRequestRO) {
//        return pushMsgService.batchPush(batchPushSendRequestRO);
//    }

//    @Override
//    public Result<Boolean> singlePush(SinglePushSendRequestRO singlePushSendRequestRO) {
//        return pushMsgService.singlePush(singlePushSendRequestRO);
//    }

    /**
     * 还款短信通知
     *
     * @param userCode
     * @param tradeNo
     * @param mobile
     * @param name
     * @param repaymentDate
     * @param borrowCapital
     * @return
     */
    @Override
    public boolean sendBillNote(String userCode, String tradeNo, String mobile, String name, Date repaymentDate, String borrowCapital) {
//        Map<String, String> params = new HashMap<>();
//        String date = DateUtil.getDate(repaymentDate, "dd-MM-yyyy");
//        SingleSmsSendRequestRO singleSmsSendRequestRO = new SingleSmsSendRequestRO();
//        // send mail
//        params.put("name", name);
//        params.put("totalAmount", borrowCapital);
//        params.put("date", date);
//        String bizCode = messageUtils.getMessageBizCodeByPackageName(userCode, tradeNo, MessageSceneEnum.SMS_REPAYMENT_REMINDER);
//        singleSmsSendRequestRO.setBizCode(bizCode);
//        singleSmsSendRequestRO.setPhone(mobile);
//        singleSmsSendRequestRO.setReplaceParam(params);
//        singleSmsSendRequestRO.setUserCode(userCode);
//        LOGGER.info("还款短信通知,{}", JSON.toJSONString(singleSmsSendRequestRO));
//        return smsMsgService.send(singleSmsSendRequestRO).getData();
        return true;
    }

    /**
     * 发送短信
     *
     * @param mobile
     * @param paramMap
     */
//    public Result<Boolean> sendMsg(String userCode, String mobile, String bizCode, Map<String, String> paramMap) {
//        SingleSmsSendRequestRO smsSendRequestRO = new SingleSmsSendRequestRO();
//        smsSendRequestRO.setPhone(mobile);
//        smsSendRequestRO.setBizCode(bizCode);
//        smsSendRequestRO.setReplaceParam(paramMap);
//        smsSendRequestRO.setUserCode(userCode);
//        Result<Boolean> send = this.send(smsSendRequestRO);
//        return send;
//    }
}
